package com.rmuti.acm.string;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class JimPhone {

	public static void main(String[] args) {
		List<String> lst = new ArrayList<>();
		int loop = 0;
		int counter = 0;
		try {
			Scanner sc = new Scanner(
					new File(
							"/home/off/git2/rmuti-programming-lab/javapattern/src/d.in"));
			while (sc.hasNext()) {
				String str = sc.nextLine();
				if (counter == 0)
					loop = Integer.parseInt(str);
				else
					lst.add(str);
				counter++;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		int lstCounter = 0;
		for (int i = 1; i <= loop; i++) {
			String firstRow = lst.get(lstCounter);
			lstCounter++;
			String[] data = firstRow.split(" ");
			int dataLoop = Integer.parseInt(data[0]);
			List<String> lstGirlName = Arrays.asList(data).subList(1, data.length);
			System.out.println("case_" + i);
			for (int j = 0; j < dataLoop; j++) {
				String dataNumber = lst.get(lstCounter);
				String[] phoneData = dataNumber.split(" ");
				String phoneName = phoneData[0];
				String phoneNumber = phoneData[1];
				String firstNameChar = phoneName.substring(0, 1);
				if (lstGirlName.contains(firstNameChar)) {
					String newPhoneNumber = "";
					int digit1 = Integer.parseInt(phoneNumber.substring(phoneNumber.length()-2, phoneNumber.length()-1));
					int digit2 = Integer.parseInt(phoneNumber.substring(phoneNumber.length()-1, phoneNumber.length()));
					if(digit1 == digit2){
						if(digit1%2==0){
							if(digit1==0){
								digit1 = 9;
							} else {
								digit1--;
							}
						} else {
							if(digit1==9){
								digit1 = 0;
							} else {
								digit1++;
							}
						}
						newPhoneNumber = phoneNumber.substring(0,phoneNumber.length()-2)+digit1+digit1;
					} else {
						newPhoneNumber = phoneNumber.substring(0,phoneNumber.length()-2)+digit2+digit1;
					}
					System.out.println(phoneName+" "+newPhoneNumber);
				}
				lstCounter++;
			}

		}

	}
}
